package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.Admin;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;


/**
 * 管理员DAO
 * @author Administrator
 *
 */
@SuppressWarnings("rawtypes")
public class AdminDaoImp implements CommnonDao{
	/**
	 * 用于保存一个管理员
	 */
	@Override
	public long save(Object value) throws Exception {
		Admin admin=null;
		if(value instanceof Admin){
			 admin=(Admin)value;
			String sql="INSERT INTO t_admin(admin_id,admin_name,admin_sex,admin_qq,admin_phone,admin_email,create_time,admin_type) VALUES(?,?,?,?,?,?,?,?)";
			int num=(int)JdbcUtil.insertAuto(sql,admin.getAdminId(),admin.getAdminName(),admin.getAdminSex(),admin.getAdminQq(),admin.getAdminPhone(),admin.getAdminEmail(),admin.getCreateTime(),admin.getAdminType());
			return num;
		}
		return 0;
	}
	/**
	 * 修改管理员
	 * 可以修改姓名，性别，QQ，电话，邮箱，
	 */
	@Override
	public int update(Object value) throws Exception {
		Admin admin=null;
		if(value instanceof Admin){
			 admin=(Admin)value;
			 String sql="UPDATE t_admin SET admin_name=?,admin_sex=?,admin_qq=?,admin_phone=?,admin_email=? where admin_name=? or admin_id=? or admin_phone=? or admin_email=? or admin_qq=?;";
			int num=JdbcUtil.update(sql, 
					admin.getAdminName(),
					admin.getAdminSex(),
					admin.getAdminQq(),
					admin.getAdminPhone(),
					admin.getAdminEmail(),
					admin.getAdminName(),
					admin.getAdminId(),
					admin.getAdminPhone(),
					admin.getAdminEmail(),
					admin.getAdminQq()
					);
			return num;
		}
		return 0;
	}
	/**
	 * 删除
	 * 后台登录日志表t_backstage_login_log，操作历史表t_backstage_operation
	 */
	@Override
	public int delete(Object value) throws Exception {
					String sql="DELETE t_admin,t_backstage_login_log,t_backstage_operation ";
							sql+="FROM t_admin ";
							sql+="LEFT JOIN t_backstage_login_log ON t_admin.admin_id=t_backstage_login_log.admin_id ";
							sql+="LEFT JOIN t_backstage_operation ON t_admin.admin_id=t_backstage_operation.admin_id ";
							sql+="WHERE t_admin.admin_id=? or t_admin.admin_name=?;";
					int num=JdbcUtil.update(sql, value,value);
		return num;	
	}
	/**
	 * 查找管理员
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Admin> findAll(int pageNo, int pageSize) throws Exception {
		String sql="select * from t_admin limit ?,?";
		return (List<Admin>)JdbcUtil.query(Admin.class, sql, (pageNo-1)*pageSize,pageSize);
	}
	
	/**
	 * 根据条件查询很多记录
	 * 性别，状态，类型，创建时间
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql="select * from t_admin where admin_sex=?  or create_time=? or admin_type=? or admin_state=? limit ?,?";
		return (List<Admin>)JdbcUtil.query(Admin.class, sql,key,key,key,key, (pageNo-1)*pageSize,pageSize);
	}
	
	/**
	 * 查找单一内容
	 * 后台登录日志表t_backstage_login_log，操作历史表t_backstage_operation
	 */
	@Override
	public Admin findByKey(Object Key) throws Exception {
		return (Admin) JdbcUtil.uniqueBean(Admin.class,
				"select * from t_admin where admin_id = ? or admin_name = ?",
					Key, Key);
	}
	
	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
